package com.zhonghui.wms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.wms.domain.WmsVehicleSchedulingDetails;
import com.zhonghui.wms.domain.vo.WmsDistributionVo;
import com.zhonghui.wms.domain.vo.WmsVehicleSchedulingVo;
import com.zhonghui.wms.mapper.WmsVehicleSchedulingMapper;
import com.zhonghui.wms.mapper.WmsWarehouseAreaMapper;
import com.zhonghui.wms.mapper.WmsWarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.zhonghui.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zhonghui.wms.domain.WmsDistributionDetails;
import com.zhonghui.wms.mapper.WmsDistributionMapper;
import com.zhonghui.wms.domain.WmsDistribution;
import com.zhonghui.wms.service.IWmsDistributionService;

/**
 * 智能仓储WMS-运输管理-配送管理Service业务层处理
 *
 * @author zhonghui
 * @date 2023-08-09
 */
@Service
public class WmsDistributionServiceImpl implements IWmsDistributionService
{
    @Autowired
    private WmsDistributionMapper wmsDistributionMapper;

    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;

    @Autowired
    private WmsWarehouseAreaMapper wmsWarehouseAreaMapper;

    @Autowired
    private WmsVehicleSchedulingMapper wmsVehicleSchedulingMapper;
    /**
     * 查询智能仓储WMS-运输管理-配送管理
     *
     * @param id 智能仓储WMS-运输管理-配送管理主键
     * @return 智能仓储WMS-运输管理-配送管理
     */
    @Override
    public WmsDistributionVo selectWmsDistributionById(Long id)
    {
        WmsDistributionVo wmsDistributionVo = wmsDistributionMapper.selectWmsDistributionById(id);
        for (WmsDistributionDetails details:wmsDistributionVo.getWmsDistributionDetailsList()) {
            details.setWmsWarehouseAreaList(wmsWarehouseMapper.selectWmsWarehouseById(details.getWarehouseId()).getWmsWarehouseAreaList());
            details.setWmsWarehouseSeatList(wmsWarehouseAreaMapper.selectWmsWarehouseAreaById(details.getWarehouseAreaId()).getWmsWarehouseSeatList());
        }
        // 判断调度ID是否为空
        if (wmsDistributionVo.getVehicleId() != null){
            // 获取调度单，防止调度单被删除后，配送管理查询失败
            WmsVehicleSchedulingVo vehicleSchedulingVo = wmsVehicleSchedulingMapper.selectWmsVehicleSchedulingById(wmsDistributionVo.getVehicleId());
            if (vehicleSchedulingVo != null){
                wmsDistributionVo.setWmsVehicleSchedulingDetailsList(vehicleSchedulingVo.getWmsVehicleSchedulingDetailsList());
            }
        }
        return wmsDistributionVo;
    }

    /**
     * 查询智能仓储WMS-运输管理-配送管理列表
     *
     * @param wmsDistributionVo 智能仓储WMS-运输管理-配送管理
     * @return 智能仓储WMS-运输管理-配送管理
     */
    @Override
    public List<WmsDistributionVo> selectWmsDistributionList(WmsDistributionVo wmsDistributionVo)
    {
        return wmsDistributionMapper.selectWmsDistributionList(wmsDistributionVo);
    }

    /**
     * 新增智能仓储WMS-运输管理-配送管理
     *
     * @param wmsDistribution 智能仓储WMS-运输管理-配送管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsDistribution(WmsDistribution wmsDistribution)
    {
        String documentNo = generateNumber("PSGL");
        wmsDistribution.setDocumentNo(documentNo);
        int rows = wmsDistributionMapper.insertWmsDistribution(wmsDistribution);
        insertWmsDistributionDetails(wmsDistribution);
        return rows;
    }

    private String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
    /**
     * 修改智能仓储WMS-运输管理-配送管理
     *
     * @param wmsDistribution 智能仓储WMS-运输管理-配送管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsDistribution(WmsDistribution wmsDistribution)
    {
        // 判断配送状态是否为已收货 0：未配送 1：配送中 2已收货
        if (wmsDistribution.getDeliveryStatus() == 2){
            // 设置到达时间
            wmsDistribution.setArrivalTime(DateUtils.getNowDate());
        }
        // 判断配送状态是否为未配送 0：未配送 1：配送中 2已收货（此判断用于修改，防止前端点击配送按钮、收货按钮清空配送管理明细）
        if (wmsDistribution.getDeliveryStatus() == 0){
            wmsDistributionMapper.deleteWmsDistributionDetailsByDistributionId(wmsDistribution.getId());
            insertWmsDistributionDetails(wmsDistribution);
        }
        return wmsDistributionMapper.updateWmsDistribution(wmsDistribution);
    }

    /**
     * 批量删除智能仓储WMS-运输管理-配送管理
     *
     * @param ids 需要删除的智能仓储WMS-运输管理-配送管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsDistributionByIds(Long[] ids)
    {
        wmsDistributionMapper.deleteWmsDistributionDetailsByDistributionIds(ids);
        return wmsDistributionMapper.deleteWmsDistributionByIds(ids);
    }

    /**
     * 删除智能仓储WMS-运输管理-配送管理信息
     *
     * @param id 智能仓储WMS-运输管理-配送管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsDistributionById(Long id)
    {
        wmsDistributionMapper.deleteWmsDistributionDetailsByDistributionId(id);
        return wmsDistributionMapper.deleteWmsDistributionById(id);
    }

    /**
     * 新增智能仓储WMS-运输管理-配送管理明细信息
     *
     * @param wmsDistribution 智能仓储WMS-运输管理-配送管理对象
     */
    public void insertWmsDistributionDetails(WmsDistribution wmsDistribution)
    {
        List<WmsDistributionDetails> wmsDistributionDetailsList = wmsDistribution.getWmsDistributionDetailsList();
        Long id = wmsDistribution.getId();
        if (StringUtils.isNotNull(wmsDistributionDetailsList))
        {
            List<WmsDistributionDetails> list = new ArrayList<WmsDistributionDetails>();
            for (WmsDistributionDetails wmsDistributionDetails : wmsDistributionDetailsList)
            {
                wmsDistributionDetails.setDistributionId(id);
                list.add(wmsDistributionDetails);
            }
            if (list.size() > 0)
            {
                wmsDistributionMapper.batchWmsDistributionDetails(list);
            }
        }
    }
}
